package com.lanyou.cook.entity.domain;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.beanutils.PropertyUtils;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lanyou.cook.common.DefaultDateJsonSerializer;
import com.lanyou.cook.entity.kettle.KettleSlaveServer;


@Entity
@EntityListeners({ AuditingEntityListener.class })
@Table(name = "  T_COOK_BU_SLAVE_SERVER_LOG")
public class SlaveServerLog{
	
	private String id;
	
	private KettleSlaveServer kettleSlaveServer;
	
	private String name;
	
	private String contents;
	
	private String creator;

	private String modifier;

	private Date createTime;

	private Date modifyTime;
	
	@CreatedBy
	@Column(length = 50, updatable=false)
	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	@LastModifiedBy
	@Column(length = 50)
	public String getModifier() {
		return modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	@Column(name = "CREATED_DATE", updatable=false)
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@CreatedDate
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "LAST_UPDATED_DATE")
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@LastModifiedDate
	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}
	
	@Id
	@GeneratedValue(generator = "uuidGenerator")
	@GenericGenerator(name = "uuidGenerator", strategy = "uuid")
	@Column(name = "SLAVE_SERVER_LOG_ID", length = 36)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	@ManyToOne
	@JoinColumn(name = "R_SLAVE_ID")
	public KettleSlaveServer getKettleSlaveServer() {
		return kettleSlaveServer;
	}

	public void setKettleSlaveServer(KettleSlaveServer kettleSlaveServer) {
		this.kettleSlaveServer = kettleSlaveServer;
	}
	@Column(name="SLAVE_SERVER_NAME", length=255)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	@Column(name="CONTENTS", length=1000)
	public String getContents() {
		return contents;
	}

	public void setContents(String contents) {
		this.contents = contents;
	}
	@Transient
	public String getKettleSlaveServiceName() {
		try {
			return (String) PropertyUtils.getProperty(kettleSlaveServer, "name");
		} catch (Exception e) {
			return null;
		}
	}
	@Transient
	public Long getKettleSlaveServiceId() {
		try {
			return (Long) PropertyUtils.getProperty(kettleSlaveServer, "id");
		} catch (Exception e) {
			return null;
		}
	}
	@Transient
	public String getKettleSlaveServicePort() {
		try {
			return (String) PropertyUtils.getProperty(kettleSlaveServer, "port");
		} catch (Exception e) {
			return null;
		}
	}
	@Transient
	public String getKettleSlaveServiceHostName() {
		try {
			return (String) PropertyUtils.getProperty(kettleSlaveServer, "hostName");
		} catch (Exception e) {
			return null;
		}
	}
	@Transient
	public Boolean getKettleSlaveServiceIsMaster() {
		try {
			return (Boolean) PropertyUtils.getProperty(kettleSlaveServer, "isMaster");
		} catch (Exception e) {
			return null;
		}
	}
	
	@Transient
	public String getCreateTimeForExport() {
		Date time = getCreateTime();
		if(time!=null){
			return time.toString();
		}
		return "";
	}
	
}
